package com.bojiu.lawsearch.lawsearch.wxzf;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Map;

@Component
@Slf4j
public class WXZFHttpHandler {
    @Autowired
    RestTemplate restTemplate;

//
//	/**
//	 * 获取公众号openid
//	 *
//	 * @param code
//	 * @return
//	 */
//	public String getGZOpenid(String code) {
//		String url = String.format(Constance.GZ_OPENIDURL, WXZFConstance.GZ_APPID, WXZFConstance.GZ_APPSECRET, code);
//		ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
//		String body = forEntity.getBody();
//		if (StringUtils.isEmpty(body)) {
//			return null;
//		}
//		String openid = JSONObject.parseObject(body).getString("openid");
//		log.debug("openid is :{}", openid);
//		if (StringUtils.isEmpty(openid)) {
//			return null;
//		} else {
//			return openid;
//		}
//
//	}

    /**
     * post请求访问
     *
     * @param data
     * @param url
     * @return
     * @throws Exception
     */
    public Map<String, String> doPost(String data, String url) throws Exception {
        log.debug("data is :{}", data);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("text/xml; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> requestEntity = new HttpEntity<String>(data, headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, requestEntity, String.class);
        String body = postForEntity.getBody();
        body = new String(body.getBytes("ISO-8859-1"), "UTF-8");
        log.debug("body is :{}", body);
        Map<String, String> processResponseXml = WXZFUtil.processResponseXml(body);
        return processResponseXml;
    }

    /**
     * 请求 需要证书
     *
     * @param data
     * @param url
     * @return
     * @throws Exception
     */
    public Map<String, String> doPostSSL(String data, String url) throws Exception {
        // 添加证书
        FileInputStream inputStream = new FileInputStream(new File(WXZFConstance.APIFILEURL));
        KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(inputStream, WXZFConstance.MCH_ID.toCharArray());
        inputStream.close();
        SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, WXZFConstance.MCH_ID.toCharArray())
                .build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
        CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(csf).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(client);
        restTemplate.setRequestFactory(requestFactory);
        // 开始请求
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("text/xml; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> requestEntity = new HttpEntity<String>(data, headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, requestEntity, String.class);
        String body = postForEntity.getBody();
        body = new String(body.getBytes("ISO-8859-1"), "UTF-8");
        log.debug("body is :{}", body);
        Map<String, String> processResponseXml = WXZFUtil.processResponseXml(body);
        return processResponseXml;
    }
}
